package com.zhaojun.netty.helloworld2;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.util.ReferenceCountUtil;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

/**
 * Created by asus on 2017/9/24.
 */
public class Client {

    public static void main(String[] args) {
        NioEventLoopGroup worker=new NioEventLoopGroup();
        Bootstrap bootstrap=new Bootstrap();

        bootstrap.group(worker).channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline()
                                .addLast(new StringEncoder())
                                .addLast(new StringDecoder())/*.addLast(new ReadTimeoutHandler(5))*/
                                .addLast(new LineBasedFrameDecoder(1024))
                                .addLast(new ChannelInboundHandlerAdapter(){
                                    @Override
                                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                        String receiveMsg= (String) msg;
                                        System.out.println(receiveMsg);
                                    }

                                    @Override
                                    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
                                        super.channelReadComplete(ctx);
                                    }

                                    @Override
                                    public void channelActive(ChannelHandlerContext ctx) throws Exception {

                                        for (int i = 0; i < 10; i++) {
        //                                    TimeUnit.SECONDS.sleep(2);
                                            String timeStr=String.valueOf(System.currentTimeMillis())+"\r\n";
//                                            System.out.println(timeStr);
                                           // ByteBuf msg = Unpooled.wrappedBuffer(timeStr.getBytes("utf-8"));
                                            ctx.writeAndFlush(timeStr);
                                        }

                                    }
                        });
                    }
                });


        try {
            ChannelFuture f = bootstrap.connect("127.0.0.1", 8888).sync();
            Channel channel = f.channel();
           /* for (int i = 0; i < 10; i++) {
                TimeUnit.SECONDS.sleep(2);
                String timeStr=String.valueOf(System.currentTimeMillis());
                ByteBuf msg = Unpooled.wrappedBuffer(timeStr.getBytes("utf-8"));
                channel.writeAndFlush(msg);
            }*/
            channel.closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
